package logic

import (
	"context"
	"xialou_platform/app/wechat"
	"xialou_platform/services/wallet/api/internal/svc"
	"xialou_platform/services/wallet/api/internal/types"
	"xialou_platform/services/wallet/rpc/walletClient"

	"github.com/zeromicro/go-zero/core/logx"
)

type BankInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewBankInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) BankInfoLogic {
	return BankInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BankInfoLogic) BankInfo(req *types.BankInfoReq) (resp *types.BankInfoResp, err error) {

	accessToken, err := l.svcCtx.WxMiniProgram.GetAuth().GetAccessToken()
	if err != nil {
		return nil, err
	}

	bankInfo, err := wechat.GetBankByNum(accessToken, req.BankNumber)
	if err != nil {
		logx.Infof("根据银行卡号查询银行信息异常: %v", err)
		return nil, err
	}
	logx.Infof("根据银行卡号查询银行信息: %v", bankInfo)

	bankIconByName, err := l.svcCtx.WalletRpc.BankIconByName(l.ctx, &walletClient.BankIconByNameReq{Name: bankInfo.Data[0].BankName})
	if err != nil {
		return nil, err
	}

	resp = &types.BankInfoResp{
		Icon:     bankIconByName.Icon,
		BankName: bankInfo.Data[0].BankName,
	}

	return resp, nil
}
